package com.zhangwei.controller;

import com.zhangwei.pojo.IsConfig;
import com.zhangwei.pojo.bean.ResultVO;
import com.zhangwei.service.IsConfigService;
import com.zhangwei.utils.ResultVoUtil;
import com.zhangwei.utils.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by zhangwei
 * 信源Controller
 */
@Controller
@RequestMapping("/manage/isconfig")
@Slf4j
public class IsConfigMController {

    @Autowired
    IsConfigService isConfigService;

    @RequestMapping("/modify")  // 修改ui
    public ModelAndView modify(@RequestParam(value = "id",required = false) int id, Map<String,Object> map){
        //map是传给模板的参数，这是一种写法
//        System.out.println("access index11111111111111111");

        if(!StringUtils.isEmpty(id)){
            IsConfig isConfig=isConfigService.selectById(id);
            map.put("isConfig",isConfig);
        }
        return new ModelAndView("/isconfig/modify",map);

    }

    @RequestMapping("/setis")   //启用配置
    @ResponseBody
    public ResultVO setIs(@RequestParam(value = "id") int is_id){

        int res=isConfigService.enableOneIs(is_id);
//        System.out.println("1111111111:"+res);
        if(res>0){
            log.info("当前启用的信源为{}",isConfigService.selectById(is_id));
            return ResultVoUtil.success();
        }

        return ResultVoUtil.error(StatusCode.UPDATE_FAILED.getCode(),"启用信源配置失败");
    }

    @RequestMapping("/addui")  // 增加ui
    public ModelAndView addui(Map<String,Object> map){
        //map是传给模板的参数，这是一种写法
//        System.out.println("access index11111111111111111");
        return new ModelAndView("/isconfig/add",map);

    }

    @RequestMapping("/list")
//    @ResponseBody   //注意是否返回模板
    public ModelAndView list(){
//        List<IsConfig> list= IsConfigService.selectAll();
//        return ResultVoUtil.success(list);
        List<IsConfig> list= isConfigService.selectAll();
        ModelAndView mav=new ModelAndView("/isconfig/list");
        mav.addObject("list",list);  //第二种传值方式
        log.info("获取信源列表成功{}",list);
        return mav;
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResultVO add(@RequestParam(value = "cname") String cname,//对应form的name
                        @RequestParam(value = "url") String url,
                        @RequestParam(value = "status") String status,
                        @RequestParam(value = "description") String description){  //参数为pojo  key-value都要用引号postman在body写 raw-json


        IsConfig isConfig=new IsConfig();
        isConfig.setCname(cname);
        isConfig.setUrl(url);
        isConfig.setStatus(status);
        isConfig.setDescription(description);

        int res= isConfigService.insertOne(isConfig);
        if(res>0){
            return ResultVoUtil.success();
        }
        return ResultVoUtil.error(1,"新增失败");
    }

    //https://blog.csdn.net/suki_rong/article/details/80445880
//    @RequestMapping("/add")
//    @ResponseBody
//    public ResultVO add(@RequestBody IsConfig IsConfig){  //参数为pojo  key-value都要用引号postman在body写 raw-json
//
//        log.info("access add");
//        System.out.println(IsConfig.toString());
//        int res= IsConfigService.insertOne(IsConfig);
//        if(res>0){
//            return ResultVoUtil.success();
//        }
//        return ResultVoUtil.error(1,"插入失败");
//    }

    // BindingResult bindingResult注意这个参数，如果在参数列表中添加这个参数会报错？？用处？
    @RequestMapping("/update")
    @ResponseBody
    public ResultVO update(@RequestParam(value = "id") int id,   //不写会err
                           @RequestParam(value = "cname",required = false) String name,//对应form的name
                           @RequestParam(value = "url") String url,
                           @RequestParam(value = "status",required = false) String status,
                           @RequestParam(value = "description") String description){

//        System.out.println("form:"+form.getCkey());
        IsConfig isConfig=new IsConfig();
        isConfig.setId(id);
        isConfig.setCname(name);
        isConfig.setUrl(url);
        isConfig.setStatus(status);
        isConfig.setDescription(description);
        int res=isConfigService.updateOne(isConfig);
        if(res>0){
            return ResultVoUtil.success();
        }
        return ResultVoUtil.error(1,"更新失败");

    }

    /**
     * ok  postman:body-raw-son  键值对json字符串  可正常测试
     * @param isConfigId
     * @return
     */
//    @RequestMapping("/update")
//    @ResponseBody
//    public ResultVO update(@RequestBody IsConfig IsConfig){
//
//        int res=IsConfigService.updateOne(IsConfig);
//        if(res>0){
//            return ResultVoUtil.success();
//        }
//        return ResultVoUtil.error(1,"更新失败");
//
//    }

    @RequestMapping("/del")
    @ResponseBody
    public ResultVO update(@RequestParam(value = "id") int isConfigId) {

        int res = isConfigService.delById(isConfigId);
        if (res > 0) {
            return ResultVoUtil.success();
        }
        return ResultVoUtil.error(1, "删除失败");
    }

    }
